package com.gexiaobao.pigeon.ui.pilot.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.TrainingUnDoneListResponse
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentReleaseMissionUndoneCompleteBinding
import com.gexiaobao.pigeon.ui.adapter.TrainingUnDoneAdapter
import com.gexiaobao.pigeon.ui.fragment.mine.organization.detail.FragmentOrgCompetition
import com.gexiaobao.pigeon.ui.pilot.activity.MissionInfoActivity
import com.gexiaobao.pigeon.viewmodel.ReleaseMissionViewModel
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 11:28
 * @Describe    : 未完成
 */
class FragmentUnDone : BaseFragment<ReleaseMissionViewModel, FragmentReleaseMissionUndoneCompleteBinding>() {

    private val mAdapter: TrainingUnDoneAdapter by lazy { TrainingUnDoneAdapter(arrayListOf()) }
    private var unDoneDataList: ArrayList<TrainingUnDoneListResponse.InfoList> = arrayListOf()

    private var mOrgId = ""

    override fun initView(savedInstanceState: Bundle?) {
        mOrgId = requireArguments().getString("orgId").toString()

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )

        mDatabind.layoutList.swipeRefreshLayout.init {
            showLoading(getString(R.string.loading))
            mViewModel.getFlyPassList(mOrgId)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                startActivity<MissionInfoActivity>(
                    Constant.TITLE to mAdapter.data[position].passName,
                    "passId" to mAdapter.data[position].id.toString(),
                    "passType" to mAdapter.data[position].passType,
                    "passStatus" to mAdapter.data[position].passStatus
                )
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.getFlyPassList(mOrgId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.trainingResult.observe(viewLifecycleOwner) {
            if (mDatabind.layoutList.swipeRefreshLayout.isRefreshing) {
                mDatabind.layoutList.swipeRefreshLayout.isRefreshing = false
            }
            dismissLoading()
            if (!it.isEmpty) {
                unDoneDataList.clear()
                it.listData?.forEachIndexed { _, infoList ->
                    if (infoList.passStatus < 6) {
                        unDoneDataList.addAll(
                            listOf(
                                TrainingUnDoneListResponse.InfoList(
                                    passName = infoList.passName,
                                    id = infoList.id,
                                    flyTime = infoList.flyTime,
                                    loadedTime = infoList.loadedTime,
                                    passType = infoList.passType,
                                    passStatus = infoList.passStatus
                                )
                            )
                        )
                    }
                }
                mAdapter.setList(unDoneDataList)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    companion object {

        fun newInstance(id: String): FragmentUnDone {
            val fragment = FragmentUnDone()
            val bundle = Bundle()
            bundle.putString("orgId", id)
            fragment.arguments = bundle
            return fragment
        }
    }
}